<?php
/**
{
    "traceId": "BIGINT //id",
    "nick": "VARCHAR //昵称@noList; ",
    "uid": "INT //用户@noload; @search; @foreign::tbl_user",
    "event": "VARCHAR //事件@search; ",
    "count": "INT //访问次数",
    "duration": "INT //时长",
    "title": "VARCHAR //标题@search; ",
    "schoolId": "INT //机构@foreign::tbl_school; @search; ",
    "ip": "VARCHAR //ip@noEdit; @noList; ",
    "ipinfo": "VARCHAR //访问地点@noEdit; @noList; ",
    "created": "DATETIME //开始时间@search; "
}
 */

class Traceext
{
    //Traceext::log($uid,$event,$title,$schoolId);
    public static function log($uid,$event,$title,$schoolId,$teacehrId=0,$courseId=0){
        self::addDailySchool($schoolId);

        
        //统一采用visitor,enter
        // $trace = new Trace();
        // $trace->uid = $uid;

        // $user = new User();
        // $user->find($uid);
        // $trace->nick = $user->nick;
        // $trace->avatar = $user->avatar;

        // $trace->schoolId = $schoolId;
        // $trace->teacherId = $teacehrId;
        // $trace->courseId = $courseId;
        // $trace->event = $event;
        // $trace->title = $title;
        // $trace->ip = myrequest::getIP();
        // $trace->ipinfo = myip::getAddresInfo();
        // $trace->save();
    }


    public static function getDailyVisitByTeacher($teacherId){
        return intval(myredis::get("visitTeacher$teacherId-".date("Ymd")));
    }

    public static function addDailyVisitTeacher($teacherId){
        $key = "visitTeacher$teacherId-".date("Ymd");
        if(!myredis::get($key)){
            $leftSecond = strtotime(date("Y-m-d 23:59:59")) -  time();
            myredis::set($key,1,$leftSecond);
        }else{
            myredis::increment($key);
        }
    }

    public static function getDailyAppByTeacher($teacherId){
        //get course
        $courseTeacher = new CourseTeacher();
        $courseTeacher->teacherId = $teacherId;
        $courseTeacher->status = STATUS_PUBLISH;
        $allCourse = $courseTeacher->all();
        $courseIds = [-1];
        foreach($allCourse as $item){
            if($item['courseId']) $courseIds[] = $item['courseId'];
        }
        $courseStr = join(",",$courseIds);

        $sql = "SELECT courseId FROM tbl_course WHERE courseId IN ($courseStr) AND   status=3";
        $course = new Course();
        $result = $course->query($sql);
        $courseIds = [-1];
        foreach($result as $item){
            if($item['courseId']) $courseIds[] = $item['courseId'];
        }
        $courseStr = join(",",$courseIds);

        $today = date('Y-m-d');
        $sql = "SELECT COUNT(1) AS NUM FROM tbl_course_appointment WHERE courseId IN ($courseStr)  AND `created` LIKE '$today%'";
        $one = $courseTeacher->one($sql);
        return $one['NUM'];

        //return intval(myredis::get("appCourse$teacherId-".date("Ymd")));
    }

    public static function addDailyAppTeacher($teacherId){
        // $key = "appCourse$teacherId-".date("Ymd");
        // if(!myredis::get($key)){
        //     $leftSecond = strtotime(date("Y-m-d 23:59:59")) -  time();
        //     myredis::set($key,1,$leftSecond);
        // }else{
        //     myredis::increment($key);
        // }
    }


    public static function getDailyAppBySchool($schoolId){
        return intval(myredis::get("appSchool$schoolId-".date("Ymd")));
    }


    public static function addDailyAppSchool($schoolId){
        $key = "appSchool$schoolId-".date("Ymd");
        if(!myredis::get($key)){
            $leftSecond = strtotime(date("Y-m-d 23:59:59")) -  time();
            myredis::set($key,1,$leftSecond);
        }else{
            myredis::increment($key);
        }
    }


    public static function getDailyBySchool($schoolId){
            return intval(myredis::get("visitSchool$schoolId-".date("Ymd")));
    }

    public static function addDailySchool($schoolId){
        $key = "visitSchool$schoolId-".date("Ymd");
         if(!myredis::get($key)){
            $leftSecond = strtotime(date("Y-m-d 23:59:59")) -  time();
            myredis::set($key,1,$leftSecond);
         }else{
             myredis::increment($key);
         }
    }
}
